package com.xysq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xysq.XYApplication;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * @author 黄东凯
 * @version 数据Adapter基类，应用里所有加载数据的Adapter都继承此类。
 */
public class BaseDataAdapter<T> extends BaseAdapter {

    protected Context context;
    protected LayoutInflater inflater;
    protected List<T> itemList = new ArrayList<T>();
    protected ImageLoader imageLoader;

    public BaseDataAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        imageLoader = XYApplication.getImageLoader();
    }

    public void addItem(T item) {
        this.itemList.add(item);
        this.notifyDataSetChanged();
    }

    public void removeItem(int position) {
        this.itemList.remove(position);
        this.notifyDataSetChanged();
    }

    public void setItem(int position, T item) {
        this.itemList.set(position, item);
        this.notifyDataSetChanged();
    }

    public void addItems(List<T> itemList) {
        this.itemList.addAll(itemList);
        this.notifyDataSetChanged();
    }

    public List<T> getItems() {
        return this.itemList;
    }

    public void setItems(List<T> itemList) {
        this.itemList.clear();
        this.itemList = itemList;
        this.notifyDataSetChanged();
    }

    public void clearItems() {
        this.itemList.clear();
        this.notifyDataSetChanged();
    }

    public boolean isEmpty() {
        return itemList.isEmpty();
    }

    @Override
    public int getCount() {
        return this.itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return null;
    }

    static class BaseViewHolder {

        public BaseViewHolder(View view) {
            ButterKnife.inject(this, view);
        }

    }

}
